<?xml version="1.0" encoding="windows-1255" ?>
<%@ page import="java.util.ArrayList" %>
<%@ page import="java.util.HashMap"%>
<%@ page import="cs236369.hw5.DBConnection"%>
<%@ page import="cs236369.hw5.InitListener" %>
<%@ page import="cs236369.hw5.Team" %>
<%@ page import="cs236369.hw5.Skill" %>
<%@ page import="cs236369.hw5.User" %>
<%@ page language="java" contentType="text/html; charset=windows-1255"
    pageEncoding="windows-1255"%>
    
<%	if(session == null || session.getAttribute("userName") == null || 
			request.getParameter("postId") == null) {
		response.setStatus(404);
		response.sendRedirect(request.getContextPath() + InitListener.welcomePage);
		return;
	}
	Team team = DBConnection.getTeam(Integer.parseInt(request.getParameter("postId")));
	if(team == null) {
		response.setStatus(400);
		out.print(DBConnection.sqlerr);
		response.setHeader("Content-Type", "text/plain");
		return;
	}
	Boolean teamOwner = ((String)session.getAttribute("userName")).equals(team.getOwner()) ? true : false;
	String disabled = teamOwner ? "" : "disabled";
%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=windows-1255" />
	<title>Team <%= team.getName() %></title>
	<!-- initialize google maps scripts and what to load -->
	<script src="googleMapScripts.js" type="text/javascript"></script>
	<script> 
		googleMapScripts_loadTasks=false;		
		googleMapScripts_loadTeams=false;		
		googleMapScripts_loadMatches=false; 
		googleMapScripts_loadUserPosts=false;
		googleMapScripts_loadSinglePostType=1;
		googleMapScripts_loadSinglePostWithID=<%= team.getId() %>;
	</script>
</head>
<body class="mapPageBody">
	<p class="subtitle">Team &nbsp; <%= team.getName() %><br /></p>
	<p class="topic">Position:</p>
	<div id="map-canvas" class="map-canvas"></div>
	
	<div class="text"><form action="#" method="post" id="editPost">
		<p class="topic">Team:&nbsp;<%= team.getName() %></p>
		<label>Maximum number of people can join the team:&nbsp;</label>
		<input type="text" name="limit" id="limit" value=<% out.print("\"" + team.getLimit() + "\" " + disabled); %> /><br />
		<label>Maximum number of people the team can evacuate:&nbsp;</label>
		<input type="text" name="capacity" id="capacity" value=<% out.print("\"" + team.getCapacity() + "\" " + disabled); %> /><br />
		<label>Description:&nbsp;</label>
		<input type="text" name="content" id="content" value=<% out.print("\"" + team.getContent() + "\" " + disabled); %> /><br />
		<label>task skills:</label> <br />
		<%
			HashMap<Integer, Integer> idToIndex = new HashMap<Integer, Integer>();
			if(teamOwner) {
				out.println("<select id=\"skill1\" onchange=\"addSkillBox(this)\">");
				out.println("<option value=\"\" disabled=\"disabled\" selected=\"selected\">Add skill</option>");
				ArrayList<Skill> skills = DBConnection.getSkills(true);
				int optIndex = 1, skillId;
				for(Skill skill : skills) {
					skillId = skill.getId();
					out.print("<option id=\"opt" + skillId + "\" value=\"" + skillId + "\"");
					if(team.doesHaveSkill(skillId)) {
						out.print(" disabled");
						idToIndex.put(skillId, optIndex);
					}
					out.println(" >" + skill.getSkill() + "</option>");
					optIndex++;
				}
				out.print("</select>");
			}
		%>
		<div id="skillsListDiv" class="text">
		<%
			Skill[] taskSkills = team.getSkills();
			int index, skillId;
			for(int i = 0; i < taskSkills.length;  i++) {
				skillId = taskSkills[i].getId();
				index = idToIndex.containsKey(skillId) ? idToIndex.get(skillId) : i + taskSkills.length;
				out.println("<p id=\"newSkill" + index + "\">\n" + taskSkills[i].getSkill());
				if(teamOwner) {
					out.println("<img src=\"images/delete.png\" onclick=\"removeSelectedSkill(" + index + ")\">");
					out.println("<input value=\"" + skillId + "\" name=\"newSkillId1\" type=\"hidden\"");
				}
				out.println("</p>");
			}
		%>
		</div>
		<br />
		<input type="hidden" name="teamId" id ="teamId" value="<%= team.getId() %>" />
		<%
			if(teamOwner) {
				out.print("<input type=\"button\" value=\"Update\" class=\"regularButton\" onclick=\"updatePost(\'UpdateTeam\', \'editPost\')\"/>");
			}
		%>
	</form>
	<form id="matching">
		<input type="hidden" name="teamId" value="<%= team.getId() %>" />
		<input type="hidden" name="command" value="match team" />
		<%
			if(teamOwner) {
				Integer matchingTeamId = (Integer) session.getAttribute("matchingTaskId");
				String matchingTeamName = (String) session.getAttribute("matchingTaskName");
				String buttonText;
				if(matchingTeamId == null || matchingTeamName == null) {
					buttonText = "Recruit the team to a task";
				} else {
					buttonText = "Recruit the team to task " + matchingTeamName;
				}
				out.println("<input type=\"button\" value=\"" + buttonText + "\" class=\"regularButton\" onclick=\"updatePost(\'UpdateTask\', \'matching\')\" />");
			}
		%>
	</form>
	<form id="memberManagment">
	<%
		if(team.isMember((String)session.getAttribute("userName"))) {
			out.print("<p class=\"topic\">Team members:</p>\n<ol>");
			for(User user : team.getMembers()) {
				out.print("<li>" + user.getFirstName() + " " + user.getLastName() + "</li>");
			}
			out.print("</ol><br />");
			if(teamOwner) {
				out.print("<input type=\"button\" class=\"regularButton\" value=\"Delete team\" onclick=\"updatePost(\'UpdateTeam\', \'memberManagment\')\"/>");
				out.println("<input value=\"delete\" name=\"command\" type=\"hidden\">");
			} else {
				out.print("<input type=\"button\" class=\"regularButton\" value=\"Leave team\" onclick=\"updatePost(\'UpdateTeam\', \'memberManagment\')\"/>");
				out.println("<input value=\"leave\" name=\"command\" type=\"hidden\">");
			}
		} else if(team.canJoin()) {
			out.print("<input type=\"button\" class=\"regularButton\" value=\"Join team\" onclick=\"updatePost(\'UpdateTeam\', \'memberManagment\')\"/>");
			out.println("<input value=\"join\" name=\"command\" type=\"hidden\">");
		} else {
			out.print("The team is full, so you can't join the team.");
		}
	%>
	<input type="hidden" name="teamId" value="<%= team.getId() %>" />
	</form></div><br />
</body>
</html>
